/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FeatureViewer.java
 *
 * Created on Dec 13, 2009, 11:14:56 PM
 */
package wekinator;

import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JToggleButton;

/**
 *
 * TODO: listen to overall wekiantor state; disable button toggle when running or recording.
 *
 * @author rebecca
 */
public class FeatureViewer extends javax.swing.JFrame {

    ParameterMiniViewer[] viewers = null;

    /** Creates new form FeatureViewer */
    public FeatureViewer() {
        initComponents();
        FeatureExtractionController.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(FeatureExtractionController.PROP_EXTRACTING)) {
                    if (FeatureExtractionController.isExtracting()) {
                        toggleExtractToView.setText("Stop extracting features");
                    } else {
                        toggleExtractToView.setText("Start extracting features");
                    }
                }
            }
        });
        WekinatorInstance.getWekinatorInstance().addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                wekInstanceChanged(evt);
            }
        });
    //TODO: add listener for learning system: when recording / running, don't enable this button.
    //Also, update me when # or names of features change!
    }

    public void setNames(String[] names) {
        panelFeatures.removeAll();
        //viewers = new ParameterMiniViewer[names.length];
        viewers = new ParameterMiniViewer[0]; //so don't have to synchronize for updateFeatures
        ParameterMiniViewer[] tmps = new ParameterMiniViewer[names.length];


        for (int i = 0; i < names.length; i++) {
            tmps[i] = new ParameterMiniViewer(names[i], 0.0);
            tmps[i].setPreferredSize(new Dimension(409, 28));

            panelFeatures.add(tmps[i]);
        }
        viewers = tmps;
        repaint();
    }

    private void wekInstanceChanged(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(WekinatorInstance.PROP_FEATURECONFIGURATION)) {
            setNames(WekinatorInstance.getWekinatorInstance().getFeatureConfiguration().getAllEnabledFeatureNames());
        }
    }

    public void updateFeatures(double[] features) {
        if (features.length == viewers.length) {
            for (int i = 0; i < viewers.length; i++) {
                viewers[i].setValue(features[i]);
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        scrollFeaturePanel = new javax.swing.JScrollPane();
        panelFeatures = new javax.swing.JPanel();
        toggleExtractToView = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Features (will update when you record examples or run)");

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        panelFeatures.setLayout(new javax.swing.BoxLayout(panelFeatures, javax.swing.BoxLayout.Y_AXIS));
        scrollFeaturePanel.setViewportView(panelFeatures);

        toggleExtractToView.setText("Start extracting features");
        toggleExtractToView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleExtractToViewActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(toggleExtractToView)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 122, Short.MAX_VALUE)
                .add(jButton1)
                .addContainerGap())
            .add(scrollFeaturePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(scrollFeaturePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(toggleExtractToView)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void toggleExtractToViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleExtractToViewActionPerformed
        if (toggleExtractToView.getModel().isSelected()) {
            FeatureExtractionController.startExtracting();
        } else {
            FeatureExtractionController.stopExtracting();
        }
}//GEN-LAST:event_toggleExtractToViewActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                FeatureViewer fv = new FeatureViewer();
                String[] names = new String[2];
                names[0] = "abc";
                names[1] = "def";
                double[] vals = {0.0, 1.0};

                fv.setNames(names);
                fv.updateFeatures(vals);

                fv.setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel panelFeatures;
    private javax.swing.JScrollPane scrollFeaturePanel;
    private javax.swing.JToggleButton toggleExtractToView;
    // End of variables declaration//GEN-END:variables
}
